package cn.tpshion.config.service.impl;

import cn.tpshion.config.common.BusinessException;
import cn.tpshion.config.common.DateUtil;
import cn.tpshion.config.common.JsonUtil;
import cn.tpshion.config.service.BuildService;
import cn.tpshion.config.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.maven.shared.invoker.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class BuildServiceImpl implements BuildService {

    @Autowired
    private WebSocketServer webSocketServer;

    @Override
    public InvocationResult build(String pomPath, String mvnPath, String id) {
        InvocationRequest request = new DefaultInvocationRequest();
        request.setPomFile(new File(pomPath));
        List<String> cmd = new ArrayList<>();
        cmd.add("clean");
        cmd.add("validate");
        cmd.add("compile");
        request.setGoals(cmd);

        Invoker invoker = new DefaultInvoker();
        invoker.setMavenHome(new File(mvnPath));
        invoker.setOutputHandler(new InvocationOutputHandler() {
            @Override
            public void consumeLine(String s) throws IOException {
                if(StringUtils.hasText(s)){
                    log.info(s);
                    webSocketServer.sendMessage(id, DateUtil.nowDate() + " " + s);
                }
            }
        });

        try {
            InvocationResult invocationResult = invoker.execute(request);
            log.info("invocationResult:{}", JsonUtil.obj2String(invocationResult));
            return invocationResult;
        } catch (MavenInvocationException e) {
            log.error("执行编译失败 id:{} errorMsg：{}", id,e.getMessage(), e);
            throw new BusinessException("01","执行编译失败 id:" + id);
        }
    }

    @Override
    public InvocationResult deploy(String pomPath, String mvnPath, String id, String skipTest) {
        InvocationRequest request = new DefaultInvocationRequest();
        request.setPomFile(new File(pomPath));
        List<String> cmd = new ArrayList<>();
        if("Y".equals(skipTest)){
            cmd.add("package -Dmaven.test.skip=true");
        }else{
            cmd.add("package");
        }
        request.setGoals(cmd);

        Invoker invoker = new DefaultInvoker();
        invoker.setMavenHome(new File(mvnPath));
        invoker.setOutputHandler(new InvocationOutputHandler() {
            @Override
            public void consumeLine(String s) throws IOException {
                if(StringUtils.hasText(s)){
                    log.info(s);
                    webSocketServer.sendMessage(id, DateUtil.nowDate() + " " + s);
                }
            }
        });

        try {
            InvocationResult invocationResult = invoker.execute(request);
            log.info("invocationResult:{}", JsonUtil.obj2String(invocationResult));
            return invocationResult;
        } catch (MavenInvocationException e) {
            log.error("执行打包失败 id:{} errorMsg：{}", id,e.getMessage(), e);
            throw new BusinessException("01","执行打包失败 id:" + id);
        }
    }
}
